package com.mes.web.controller.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import com.mes.biz.domain.BizProduct;
import com.mes.biz.domain.BizReceiveOrderDetail;
import com.mes.biz.service.BizProductService;
import com.mes.biz.service.BizReceiveOrderDetailService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizReceiveOrder;
import com.mes.biz.service.BizReceiveOrderService;

/**
 * 领用出库单Controller
 * @author wy
 * @email wy
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/bizReceiveOrder")
public class BizReceiveOrderController extends BaseController {

    @Autowired
    private BizReceiveOrderService bizReceiveOrderService;

    @Autowired
    private BizReceiveOrderDetailService bizReceiveOrderDetailService;

    @Resource
    private BizProductService bizProductService;


    /**
     * 查询领用出库单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizReceiveOrder bizReceiveOrder, HttpServletRequest request, HttpServletResponse response) {
        bizReceiveOrder.setPage(new PageDomain(request, response));
        return R.data(bizReceiveOrderService.findPage(bizReceiveOrder));
    }

    /**
     * 获取领用出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:query')")
    @GetMapping(value = "/{id}")
    public R<BizReceiveOrder> detail(@PathVariable("id") String id) {
        BizReceiveOrder bizReceiveOrder = bizReceiveOrderService.get(id);
        BizReceiveOrderDetail detail = new BizReceiveOrderDetail();
        detail.setReceiveOrderId(id);
        List<BizReceiveOrderDetail> list = bizReceiveOrderDetailService.findList(detail);
        for (BizReceiveOrderDetail bizReceiveOrderDetail : list) {
            BizProduct bizProduct = bizProductService.getBizProductByProductCode(bizReceiveOrderDetail.getProductCode());
            bizReceiveOrderDetail.setBrand(bizProduct.getBrand());
            bizReceiveOrderDetail.setSpec(bizProduct.getSpec());
            bizReceiveOrderDetail.setColor(bizProduct.getColor());
            bizReceiveOrderDetail.setProductId(bizProduct.getId());
            List<Map<String,Object>> utilList = new ArrayList<>();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("key",1);
            map.put("name",bizProduct.getUnit());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",2);
            map.put("name",bizProduct.getAssistUnitA());
            utilList.add(map);
            map = new HashMap<String,Object>();
            map.put("key",3);
            map.put("name",bizProduct.getAssistUnitB());
            utilList.add(map);
            bizReceiveOrderDetail.setUnitList(utilList);
        }
        bizReceiveOrder.setDetailList(list);
        return R.data(bizReceiveOrder);
    }

    /**
     * 新增领用出库单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:add')")
    @Log(title = "领用出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizReceiveOrder bizReceiveOrder) {
        return R.status(bizReceiveOrderService.insertBizReceiveOrder(bizReceiveOrder));
    }

    /**
     * 修改领用出库单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:edit')")
    @Log(title = "领用出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizReceiveOrder bizReceiveOrder) {
        return R.status(bizReceiveOrderService.updateBizReceiveOrder(bizReceiveOrder));
    }

    /**
     * 删除领用出库单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:remove')")
    @Log(title = "领用出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizReceiveOrderService.deleteBizReceiveOrderByIds(ids));
    }


    /**
     * 导出领用出库单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizReceiveOrder:export')")
    @Log(title = "领用出库单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizReceiveOrder bizReceiveOrder) {
        List<BizReceiveOrder> list = bizReceiveOrderService.findList(bizReceiveOrder);
        ExcelUtil<BizReceiveOrder> util = new ExcelUtil<BizReceiveOrder>(BizReceiveOrder.class);
        return util.exportExcel(list, "领用出库单数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
     * 审核
     */
    @GetMapping(value = "/audit/{id}")
    public R auditOrder(@PathVariable String id) {
        return R.status(bizReceiveOrderService.auditBizReceiveOrder(id));
    }

    /**
     * 反审核
     */
    @GetMapping(value = "/doAudit/{id}")
    public R doAuditOrder(@PathVariable String id) {
        return R.status(bizReceiveOrderService.doAuditBizReceiveOrder(id));
    }

}
